package com.baihan.angus.app.game.basic.bindingAdp.view_click;

import android.annotation.SuppressLint;
import android.view.View;

import androidx.databinding.BindingAdapter;

import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;

public class ViewClickBindingAdp {
    private static long CLICK_INTERVAL = 1000;

    @SuppressLint("CheckResult")
    @BindingAdapter(value = {"onBindingClick", "isThrottleFirst"}, requireAll = false)
    public static void onClick(final View view, final ClickEvent clickEvent, final boolean isThrottleFirst) {
        if (isThrottleFirst) {
            RxView.clicks(view)
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            clickEvent.onClick(view);
                        }
                    });
        } else {
            RxView.clicks(view)
                    .throttleFirst(CLICK_INTERVAL, TimeUnit.MILLISECONDS)//1秒钟内只允许点击1次
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            clickEvent.onClick(view);
                        }
                    });
        }
    }
}
